#include "mainwindow.h"


MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    timer=new QTimer(this);
    connect(timer,&QTimer::timeout,this,&MainWindow::TimerGetGloAlm);
    timer->start(5*60*1000);        //每隔5分钟获取一次GLO历书
    TimerGetGloAlm();

}

MainWindow::~MainWindow()
{
}

void MainWindow::TimerGetGloAlm()
{
    QUrl url(URLSTR);
    qDebug() << "Reading html code form " << URLSTR;
    reply = manager.get(QNetworkRequest(url));
    //请求结束并下载完成后，退出子事件循环
    QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
    //开启子事件循环
    loop.exec();

    //获取源码，打开文件
    QByteArray codeContent = reply->readAll();
    QByteArray  array=codeContent.mid(codeContent.indexOf("GLONASS ALMANAC"));
    DecodeAlm(array.mid(0,array.indexOf("</TABLE>")));

    qDebug() << "Finished, the code have written to ";
}

void MainWindow::DecodeAlm(const QByteArray &data)
{
    memset(almanac,0,sizeof(almanac));
    QStringList strlist=QString(data).split("</td></tr>\r\n<tr bgcolor=\"#FFFFFF\">\n");
    for(int i=1;i<strlist.count();i++)
    {
        QString str1=QString(strlist[i]).replace(QRegExp("</td>\n{1,}"), " ");
        QStringList str2=str1.split("<td>",QString::SkipEmptyParts);
        if(i>=1)    //开始解析
        {
            almanac[i-1].M=1;
            almanac[i-1].n=str2.at(0).toInt();
            almanac[i-1].H=str2.at(9).toInt();
            almanac[i-1].l=0;
            almanac[i-1].Cn=1;
            almanac[i-1].tau=str2.at(8).toDouble();
            almanac[i-1].lambda=str2.at(6).toDouble();
            almanac[i-1].delta_i=str2.at(5).toDouble()-63;
            almanac[i-1].epsilon=str2.at(4).toDouble();
            almanac[i-1].omega=str2.at(7).toDouble();
            almanac[i-1].t=str2.at(2).toDouble();
            almanac[i-1].delta_T=str2.at(3).toDouble()-43200;
            if(i==24)
            {
                QStringList laststr=QString(str2[10]).split("</td></tr>\r\n");
                almanac[i-1].delta_T_dot=laststr.at(0).toDouble();
                cout<<qSetRealNumberPrecision(15)<<almanac[i-1].delta_T_dot;
            }
            else
            {
                almanac[i-1].delta_T_dot=str2.at(10).toDouble();
            }
        }
    }
}

